<template>
    <div id="user-edit">
        <Form ref="formValidate" :label-width="100" :model="parms" :rules="ruleValidate" label-position="right" @keydown.native.enter="saveAdmins">
            <FormItem label="用户名:" prop="title">
                <Input v-model="parms.title" placeholder="请输入用户名"/>
            </FormItem>
            <FormItem label="邮箱:" prop="deputyTitle">
                <Input v-model="parms.deputyTitle" placeholder="请输入邮箱"/>
            </FormItem>
            <FormItem label="密码:" prop="content">
                <Input v-model="parms.content" placeholder="请输入密码"/>
            </FormItem>
            <FormItem label="性别:" prop="category">
                <RadioGroup v-model="parms.category">
                    <Radio label="男"></Radio>
                    <Radio label="女"></Radio>
                </RadioGroup>
            </FormItem>
        </Form>
        <div style="text-align: center">
            <Button type="primary" @click="saveAdmins" @keyup.enter.native="saveAdmins">保存</Button>
        </div>
    </div>
</template>

<script>
import {deepCopy} from "view-design/src/utils/assist";
import {extend} from "@/libs/tools";

export default {
    name: "user-edit",
    props: ['selectItem'],
    data() {
        return {
            parms: {
                id: '',
                title: '',
                deputyTitle: '',
                content:'',
                category: '',
            },
            enterpriseList: [],
            ruleValidate: {
                title: [{required: true, message: '请输入用户名', trigger: 'blur'}],
                deputyTitle: [{required: true, message: '请输入邮箱', trigger: 'blur'}],
                content: [{required: true, message: '请输入密码', trigger: 'blur'}],
            }
        };
    },
    mounted() {
        this.parms = extend(this.parms, this.selectItem)
    },
    methods: {
        //添加用户
        saveAdmins() {
            this.$refs.formValidate.validate(es => {
                if (!es) return
                // 效验邮箱格式
                if (!/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(this.parms.deputyTitle)) {
                    this.$Message.error('邮箱格式不正确');
                    return;
                }
                // 效验密码强度
                if (!/^(?=.*[a-zA-Z])(?=.*\d)[^]{6,16}$/.test(this.parms.content)) {
                    this.$Message.error('密码强度太弱,需包含字母和数字');
                    return;
                }
                let parms = deepCopy(this.parms);
                if (Object.keys(this.selectItem).length > 0) {
                    this.$emit('edit', parms)
                } else {
                    this.$emit('add', parms)
                }
            })
        }
    }
};
</script>
<style scoped>

</style>
